package com.rd.p2p.mobile;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Button;
import com.foxinmy.weixin4j.mp.WeixinProxy;
import com.foxinmy.weixin4j.mp.api.OauthApi;
import com.foxinmy.weixin4j.mp.model.OauthToken;
import com.foxinmy.weixin4j.type.ButtonType;
import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.dal.dto.User;

/**
 * 微信的回调API
 */
public class MobileWeixinAction extends BaseAction<User> implements ModelDriven<User> {
	private static final Logger logger = LoggerFactory.getLogger(MobileWeixinAction.class);

	@Action("/mobile/weixin/api")
	public void weixinApi() throws Exception {
		OauthApi oauthApi = new OauthApi();
		OauthToken oauthToken = oauthApi.getOauthToken(paramString("code"));
		String openId = "";

		if (null != oauthToken) {
			logger.info("获取用户微信信息" + oauthToken);
			openId = oauthToken.getOpenId();
		}
		request.getSession().setAttribute("openId", openId);
		redirect("/mobile/usercenter/main.html");
	}

	public static void main(String[] args) throws WeixinException {
		// WeixinProxy wp = new WeixinProxy(appID, secret);

		WeixinProxy wp = new WeixinProxy();
		// System.err.println(wp.getTokenHolder().getToken());
		//
		// TemplateMessage tm = new TemplateMessage("ot-GswuQjom5xNbb1-VeymP4-Ois",
		// "Kvmv_ZMzaptWPdXG-KYkeyuc9eZcwfAzT-6nk0TNMHQ", "测试的哦",
		// "http://wx.ourjl.com/mobile/user/login.html");
		// tm.pushData("username", "wuhuan");
		// tm.pushData("product", "xxx牌机器人");
		// wp.sendTmplMessage(tm);
		List<Button> btnList = new ArrayList<Button>();
		Button main1 = new Button("安全理财", "", ButtonType.click);
		List<Button> subs1 = new ArrayList<Button>();
		Button sub11 = new Button("趣豆计划", "http://wx.ourjl.com/mobile/invest/index.html", ButtonType.view);
		Button sub12 = new Button("趣影计划", "http://wx.ourjl.com/mobile/filminvest/index.html", ButtonType.view);
		subs1.add(sub11);
		subs1.add(sub12);
		main1.setSubs(subs1);
		Button main2 = new Button("账户注册", "", ButtonType.click);
		Button sub21 = new Button("贺卡", "http://wx.ourjl.com/mobile/cardsend.html", ButtonType.view);
		Button sub23 = new Button("注册", "http://wx.ourjl.com/mobile/user/register.html", ButtonType.view);
		Button sub24 = new Button("登出", "http://wx.ourjl.com/app/user/logout.html", ButtonType.view);
		Button sub22 = new Button("我的账户", "http://wx.ourjl.com/mobile/usercenter/main.html", ButtonType.view);
		btnList.add(main1);
		btnList.add(main2);
		List<Button> subs2 = new ArrayList<Button>();
		subs2.add(sub21);
		subs2.add(sub23);
		subs2.add(sub22);
		subs2.add(sub24);
		main2.setSubs(subs2);
		try {
			wp.createMenu(btnList);
		} catch (WeixinException e) {
			e.printStackTrace();
		}
	}
}
